package com.health.ts.controller.transfer;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.consultation.PatientInfo;
import com.health.ts.entity.transfer.ReceiveInfo;
import com.health.ts.entity.transfer.Transfer;
import com.health.ts.entity.transfer.VTransferDone;
import com.health.ts.entity.transfer.VTransferList;
import com.health.ts.service.consultation.PatientInfoService;
import com.health.ts.service.transfer.ReceiveInfoService;
import com.health.ts.service.transfer.TransferService;
import com.health.ts.service.transfer.VTransferDoneService;
import com.health.ts.service.transfer.VTransferListService;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.GenerateIdUtil;
import com.health.ts.utils.SessionUserUtil;
/**  
* @ClassName: patientController  
* @Description:双向转诊管理
*/  
@RestController
@RequestMapping(value = "/api/transfer",produces= BaseController.JSON_UTF8)
public class TransferController extends BaseController{
	
	@Autowired
	TransferService transferService;
	
	@Autowired
	VTransferListService vtransferListService;
	
	@Autowired
	ReceiveInfoService  receiveInfoService;
	
	@Autowired
	VTransferDoneService  vtransferDoneService;
	
	@Autowired
	PatientInfoService  patientInfoService;
	
	/**
	 * 转诊
	 * @param transfer
	 * @return
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ApiResult addTransfer(@RequestBody Transfer transfer) {
		transfer.setId(GenerateIdUtil.makeID());
		transfer.setApply_docid(SessionUserUtil.getUserId());
		transfer.setApply_hid(SessionUserUtil.getUserHosId());
		transfer.setApply_did(SessionUserUtil.getUserDeptId());
		transfer.setApply_time(new Date());
		transfer.setPatient_id(transfer.getPatient_id());
		transfer.setAssign_hid(transfer.getAssign_hid());
		transfer.setAssign_did(transfer.getAssign_did());
		transfer.setIsDone("0");//未接诊
		transfer.setIsAgree("0");//未审核
		boolean isSucess=transferService.save(transfer);
		if(isSucess){
			PatientInfo patientInfo=new PatientInfo();
			patientInfo.setId(transfer.getPatient_id());
			patientInfo.setIsTran("1");//患者已转诊
			patientInfoService.updateById(patientInfo);
		}
		return ApiResult.success();
	}
	
	/**
	 * 转诊审核通过
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/review/{id}", method=RequestMethod.PUT)
	public ApiResult reviewTransfer(@PathVariable String id,@RequestBody Transfer transfer) {
		Transfer info = transferService.getById(id);
		AssertUtil.notNull(info, "要审核的转诊不存在!");
		transfer.setId(id);
		transfer.setIsAgree("1");
		transfer.setAgreeStatus("1");//审核通过
		transferService.updateById(transfer);
		return ApiResult.success();
	}
	
	/**
	 * 转诊审核不通过
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/unreview", method=RequestMethod.PUT)
	public ApiResult unreviewTransfer(@RequestBody Transfer transfer) {
		Transfer info = transferService.getById(transfer.getId());
		AssertUtil.notNull(info, "要更新的转诊不存在!");
		transfer.setIsAgree("1");
		transfer.setAgreeStatus("2");//审核不通过
		boolean isSucess=transferService.updateById(transfer);
		if(isSucess){
			PatientInfo patientInfo=new PatientInfo();
			patientInfo.setId(transfer.getPatient_id());
			patientInfo.setIsTran("0");//接诊后患者状态置为未转诊
			patientInfoService.updateById(patientInfo);
		}
		return ApiResult.success();
	}
	
	
	/**
	 * 医生接诊
	 * @param id
	 * @param transfer
	 * @return
	 */
	@RequestMapping(value="/agree/{id}/{pid}", method=RequestMethod.POST)
	public ApiResult agreeTransfer(@PathVariable String id,@PathVariable String pid) {
		ReceiveInfo receiveInfo=new ReceiveInfo();
		receiveInfo.setId(GenerateIdUtil.makeID());
		receiveInfo.setTid(id);
		receiveInfo.setReceive_hid(SessionUserUtil.getUserHosId());
		receiveInfo.setReceive_did(SessionUserUtil.getUserDeptId());
		receiveInfo.setReceive_docid(SessionUserUtil.getUserId());
		receiveInfo.setReceive_time(new Date());
		receiveInfo.setPid(pid);
		//增加患者已转诊标记
		boolean isSucess=receiveInfoService.save(receiveInfo);
		if(isSucess){
			Transfer transfer=new Transfer();
			transfer.setId(receiveInfo.getTid());
			transfer.setTran_time(new Date());
			transfer.setIsDone("1");
			transferService.updateById(transfer);
			PatientInfo patientInfo=new PatientInfo();
			patientInfo.setId(pid);
			patientInfo.setIsTran("0");//接诊后患者状态置为未转诊
			patientInfoService.updateById(patientInfo);
		}
		return ApiResult.success();
	}
	
	
	/**  
	* @Title: waitTransferDone  
	* @Description: 待接
	* @param @param page
	* @param @return 参数  
	* @return String 返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public ApiResult waitTransferDone(FrontPage page){
		QueryWrapper<VTransferList> QueryWrapper = new QueryWrapper<VTransferList>();
		//名称
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			QueryWrapper.like("patient_name", patient_name); 
		}
		//身份证号
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			QueryWrapper.eq("idnumber", idnumber); 
		}
		//转诊类型
		String transfer_type =getParam(page,"transfer_type");
		if (StringUtils.isNotEmpty(transfer_type)&&!"1".equals(transfer_type)){
			QueryWrapper.eq("transfer_type", transfer_type); 
		}
		QueryWrapper.eq("assign_docid", SessionUserUtil.getUserId()); 
		QueryWrapper.eq("isDone", "0"); 
		QueryWrapper.eq("isAgree", "1"); 
		QueryWrapper.eq("agreeStatus", "1");
		Page<VTransferList> pageList = vtransferListService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VTransferList> customPage = new CustomPage<VTransferList>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 由当前医生申请未接诊
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/mylist", method=RequestMethod.POST)
	public ApiResult waitMyListDone(FrontPage page){
		QueryWrapper<VTransferList> QueryWrapper = new QueryWrapper<VTransferList>();
		//名称
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			QueryWrapper.like("patient_name", patient_name); 
		}
		//身份证号
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			QueryWrapper.eq("idnumber", idnumber); 
		}
		//转诊类型
		String transfer_type =getParam(page,"transfer_type");
		if (StringUtils.isNotEmpty(transfer_type)&&!"1".equals(transfer_type)){
			QueryWrapper.eq("transfer_type", transfer_type); 
		}
		QueryWrapper.eq("apply_docid", SessionUserUtil.getUserId()); 
		Page<VTransferList> pageList = vtransferListService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VTransferList> customPage = new CustomPage<VTransferList>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 待审核列表
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listAgree", method=RequestMethod.POST)
	public ApiResult waitTransferAgree(FrontPage page){
		QueryWrapper<VTransferList> QueryWrapper = new QueryWrapper<VTransferList>();
		//名称
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			QueryWrapper.like("patient_name", patient_name); 
		}
		//身份证号
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			QueryWrapper.eq("idnumber", idnumber); 
		}
		//转诊类型
		String transfer_type =getParam(page,"transfer_type");
		if (StringUtils.isNotEmpty(transfer_type)&&!"1".equals(transfer_type)){
			QueryWrapper.eq("transfer_type", transfer_type); 
		}
		QueryWrapper.eq("assign_hid", SessionUserUtil.getUserHosId());//只有目标医院的审核管理员可以审核的转诊 
		QueryWrapper.eq("isDone", "0"); 
		QueryWrapper.eq("isAgree", "0"); 
		QueryWrapper.orderByAsc("apply_time");
		Page<VTransferList> pageList = vtransferListService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VTransferList> customPage = new CustomPage<VTransferList>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 已接诊
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listDone", method=RequestMethod.POST)
	public ApiResult transferDone(FrontPage page){
		QueryWrapper<VTransferDone> QueryWrapper = new QueryWrapper<VTransferDone>();
		String patient_name =getParam(page,"patient_name1");
		if (StringUtils.isNotEmpty(patient_name)){
			QueryWrapper.like("patient_name", patient_name); 
		}
		//身份证号
		String idnumber =getParam(page,"idnumber1");
		if (StringUtils.isNotEmpty(idnumber)){
			QueryWrapper.eq("idnumber", idnumber); 
		}
		//转诊类型
		String transfer_type =getParam(page,"transfer_type1");
		if (StringUtils.isNotEmpty(transfer_type)&&!"1".equals(transfer_type)){
			QueryWrapper.eq("transfer_type", transfer_type); 
		}
		//QueryWrapper.eq("isAgree", "1"); //已通过审核但是未接诊
		//QueryWrapper.eq("isDone", "0"); 
		QueryWrapper.orderByAsc("receive_time");
		Page<VTransferDone> pageList = vtransferDoneService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VTransferDone> customPage = new CustomPage<VTransferDone>(pageList);
		return ApiResult.success(customPage);
	}
	
	@RequestMapping(value="/mylistDone", method=RequestMethod.POST)
	public ApiResult myListDone(FrontPage page){
		QueryWrapper<VTransferList> QueryWrapper = new QueryWrapper<VTransferList>();
		String patient_name =getParam(page,"patient_name1");
		if (StringUtils.isNotEmpty(patient_name)){
			QueryWrapper.like("patient_name", patient_name); 
		}
		//身份证号
		String idnumber =getParam(page,"idnumber1");
		if (StringUtils.isNotEmpty(idnumber)){
			QueryWrapper.eq("idnumber", idnumber); 
		}
		//转诊类型
		String transfer_type =getParam(page,"transfer_type1");
		if (StringUtils.isNotEmpty(transfer_type)&&!"1".equals(transfer_type)){
			QueryWrapper.eq("transfer_type", transfer_type); 
		}
		QueryWrapper.eq("isAgree", "1"); //已通过审核但是未接诊
		QueryWrapper.eq("isDone", "0"); 
		QueryWrapper.eq("agreeStatus", "1");
		Page<VTransferList> pageList = vtransferListService.page(page.getPagePlus(), QueryWrapper);
		CustomPage<VTransferList> customPage = new CustomPage<VTransferList>(pageList);
		return ApiResult.success(customPage);
	}
	
	
	/**
	 * 查询转诊申请
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/select/{id}", method=RequestMethod.GET)
	public ApiResult selectTransfer(@PathVariable String id) {
		Transfer transfer=transferService.getById(id);
		AssertUtil.notNull(transfer, "要查询的转诊不存在!");
		return ApiResult.success();
	}
	//查询转诊列表
	@RequestMapping(value="/selectList/{id}", method=RequestMethod.GET)
	public ApiResult selectTransferList(@PathVariable String id) {
		VTransferList transfer=vtransferListService.getById(id);
		AssertUtil.notNull(transfer, "要查询的转诊不存在!");
		return ApiResult.success(transfer);
	}
	
}
